/* eslint-disable @typescript-eslint/no-unused-vars */
export interface LngLat {
	lng?: number;
	lat?: number;
	address?: string;

	[key: string]: any;
}

export interface MarkerDataInfo {
	location: LngLat;

	[key: string]: any;
}

let geocoderTool: any;
let searchTool: any;
export const convertGPS = (gps: Array<any>) => {
	return new Promise<Array<any>>((resolve, reject) => {
		window.AMap.convertFrom(gps, "gps", function (status, result: any) {
			resolve(result);
		});
	});
};

export const getAddress = (LngLat: LngLat): Promise<LngLat> => {
	if (!geocoderTool && window.AMap) {
		geocoderTool = new (window.AMap as any).Geocoder();
	}

	return new Promise<LngLat>((resolve, reject) => {
		if (!geocoderTool) {
			resolve({});
		}
		geocoderTool.getAddress(LngLat, function (status: any, result: any) {
			if (status === "complete" && result.info === "OK") {
				// result为对应的地理位置详细信息
				resolve({ lng: LngLat.lng, lat: LngLat.lat, address: result.regeocode.formattedAddress });
			} else {
				resolve({});
			}
		});
	});
};

export const searchPlace = () => {
	if (!searchTool && window.AMap) {
		searchTool = new (window.AMap as any).PlaceSearch();
	}
	if (!searchTool) {
		console.warn("暂未加载搜索插件");
		return;
	}
	searchTool.search("东方明珠", function (status: any, result: any) {
		// 查询成功时，result即对应匹配的POI信息
		console.log(status, result);
	});
};
